package com.ilarele.shopping.client.activities;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;

import com.flurry.android.FlurryAgent;
import com.ilarele.shopping.client.R;
import com.ilarele.shopping.client.util.Communication;
import com.ilarele.shopping.client.util.DBUtil;
import com.ilarele.shopping.client.util.Global;

public class A3SettingsActivity extends Activity {
	protected static final int RESULT_MAP_VIEW = 0;
	private String mLocation;
	private float mLatitude;
	private float mLongitude;
	private Button locationBtn;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.a3_settings);

		final EditText nameET = (EditText) findViewById(R.id.client_name);
		nameET.setText(Global.clientName);
		final EditText emailET = (EditText) findViewById(R.id.client_email);
		emailET.setText(Global.clientEmail);
		
		final CheckBox privacyCB = (CheckBox) findViewById(R.id.client_privacy);
		privacyCB.setChecked(Global.isPublic);
		privacyCB.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				Communication.sendPrivacy(isChecked);
			}
		});
		
		locationBtn = (Button) findViewById(R.id.client_set_location);
		locationBtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// open gmap
				// open a MapView
				Intent intent = new Intent(A3SettingsActivity.this,
						CustomEditMapView.class);
				startActivityForResult(intent, RESULT_MAP_VIEW);
			}
		});
		loadLocationFromPref();
		locationBtn.setText(mLocation);

		Button save = (Button) findViewById(R.id.save);
		save.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg0) {
				Global.clientName = nameET.getText().toString();
				Global.editor.putString("name", Global.clientName);

				Global.clientEmail = emailET.getText().toString();
				Global.editor.putString("email", Global.clientEmail);

				Global.editor.putString("location", mLocation);
				Global.editor.putFloat("latitude", mLatitude);
				Global.editor.putFloat("longitude", mLongitude);
				Global.editor.commit();
			}
		});

		Button clear = (Button) findViewById(R.id.clear_db);
		clear.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg0) {
				DBUtil.clearDB(A3SettingsActivity.this);
			}
		});
		
	}

	private void loadLocationFromPref() {
		mLatitude = Global.pref.getFloat("latitude", 0);
		mLongitude = Global.pref.getFloat("longitude", 0);
		mLocation = Global.pref.getString("location",
				getString(R.string.set_location));
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode,
			Intent intent) {
		if (resultCode == RESULT_OK) {
			if (requestCode == RESULT_MAP_VIEW) {
				mLocation = intent.getStringExtra("location");
				if (mLocation != null) {
					mLatitude = (float) intent.getDoubleExtra("latitude", 0);
					mLongitude = (float) intent.getDoubleExtra("longitude", 0);
					locationBtn.setText(mLocation);
				}
			}
		}
		super.onActivityResult(requestCode, resultCode, intent);
	}

	@Override
	public void onStart() {
		super.onStart();
		if (!Global.DEBUG_MODE)
			FlurryAgent.onStartSession(this, Global.FLURRY_ID);
	}

	@Override
	public void onStop() {
		super.onStop();
		if (!Global.DEBUG_MODE)
			FlurryAgent.onEndSession(this);
	}
}
